//
//  NetworkingTools.swift
//  AFNetworkTest
//
//  Created by baggio on 2017/8/12.
//  Copyright © 2017年 baggio. All rights reserved.
//

import UIKit
import AFNetworking

enum BANetworkingToolsType:String{
    case get="get"
    case post="post"
}

class BANetworkingTools:AFHTTPSessionManager {

    //单例，单例的写法类似于懒加载
    //static let share:BANetworkingTools=BANetworkingTools()
    
    static let share:BANetworkingTools={
        let tools=BANetworkingTools()
        
        // 默认只支持 json，如果有多种格式，可以多次 insert
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    /*
     封装框架中的 get 和 post 方法，参数根据框架中的 get 方法和 post 方法需要的参数
     get(<#T##URLString: String##String#>, parameters: <#T##Any?#>, progress: <#T##((Progress) -> Void)?##((Progress) -> Void)?##(Progress) -> Void#>, success: ((URLSessionDataTask, Any?) -> Void)?, failure: <#T##((URLSessionDataTask?, Error) -> Void)?##((URLSessionDataTask?, Error) -> Void)?##(URLSessionDataTask?, Error) -> Void#>)
     progress 不要，URLSessionDataTask? 不要
     */
    func request(type:BANetworkingToolsType,urlString:String,parameters: Any?,success:@escaping (Any?)->(),failure:@escaping (Error)->()) {
 
        if type==BANetworkingToolsType.get{
            get(urlString, parameters: parameters, progress: nil, success: { (_, reponse) in
                success(reponse)
            }, failure: { (_, error) in // , failure: 可省略
                failure(error)
            })
        }
        
//        if type==BANetworkingToolsType.get{
//            get(urlString, parameters: parameters, progress: nil, success: { (_, reponse) in
//                success(reponse)
//            }) { (_, error) in //尾随闭包的简写
//                failure(error)
//            }
//        }
        
        if type==BANetworkingToolsType.post{
            post(urlString, parameters: parameters, progress: nil, success: { (_, response) in
                success(response)
            }, failure: { (_, error) in
                failure(error)
            })
        }
    }
}



//Home 模块
extension BANetworkingTools {
    
    //需要加2个参数 since_id 和 max_id
//    func loadHomeData(success: @escaping (Any?) -> (),failure: @escaping (Error) -> ()) {
//        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
//        let params = [
//            "access_token": BAOAuthViewModel.share.oAuthModel?.access_token
//        ]
//        
//        request(type: .get, urlString: urlString, parameters: params, success: success, failure: failure)
//    }
    
    func loadHomeData(since_id: Int64, max_id: Int64, success: @escaping (Any?) -> (),failure: @escaping (Error) -> ()) {
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let params = [
            "access_token": BAOAuthViewModel.share.oAuthModel?.access_token,
            "since_id": "\(since_id)",
            "max_id": "\(max_id)"
        ]
        
        request(type: .get, urlString: urlString, parameters: params, success: success, failure: failure)
    }
}

//OAuth 模块
extension BANetworkingTools {
    
    func loadOAuthAccessToken(code:String,success:@escaping (Any?)->(),failure:@escaping (Error)->()){
        let urlString="https://api.weibo.com/oauth2/access_token"
        let params=[
            "client_id":BA_Weibo_AppKey,
            "client_secret":BA_Weibo_AppSecret,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":BA_Weibo_Redirect_Uri
        ]
        request(type: .post, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    
    func loadOAuthUserInfo(oAuthModel:BAOAuthModel,success:@escaping (Any?)->(),failure:@escaping (Error)->()){
        let urlString="https://api.weibo.com/2/users/show.json"
        let params=[
            "access_token":oAuthModel.access_token,
            "uid":oAuthModel.uid
        ]
        request(type: .get, urlString: urlString, parameters: params, success: success, failure: failure)
    }
}
